package com.kh.pajx.sop.util.common;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

/**
 * 中文转换为拼音
 * 
 * @author lipengfei2014/7/9
 * 
 */
public class ChineseToEnglish {

	public static String getPingYin(String src) {
		// 将汉字转换为全拼
		char[] t1 = null;
		t1 = src.toCharArray();
		String[] t2 = new String[t1.length];
		HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();

		t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
		t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		t3.setVCharType(HanyuPinyinVCharType.WITH_V);
		String t4 = "";
		int t0 = t1.length;
		try {
			for (int i = 0; i < t0; i++) {
				// 判断是否为汉字字符
				if (java.lang.Character.toString(t1[i]).matches(
						"[\\u4E00-\\u9FA5]+")) {
					t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);
					t4 += t2[0];
				} else
					t4 += java.lang.Character.toString(t1[i]);
			}
			return t4;
		} catch (BadHanyuPinyinOutputFormatCombination e1) {
			e1.printStackTrace();
		}
		return t4;
	}

	
	
	/**
	 * 汉字转换汉语拼音首字母，英文字符不变 
	 * 例如：北京 --> BJ
	 * @param chinese 待转换的汉字
	 * @return String 汉字简拼
	 * @author azzcsyu
	 * @update 2013-12-19 下午2:46:29
	 */
	public static String cn2FirstSpell(String chinese) {
		StringBuilder pinyin = new StringBuilder();
		char[] nameChar = chinese.toCharArray();
		HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
		defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
		defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		for (int i = 0; i < nameChar.length; i++) {
			if (nameChar[i] > 128) {
				try {
					String[] py = PinyinHelper.toHanyuPinyinStringArray(
							nameChar[i], defaultFormat);
					if (py != null) {
						pinyin.append(py[0].charAt(0));
					}
				} catch (BadHanyuPinyinOutputFormatCombination e) {
					e.printStackTrace();

				}

			} else {
				pinyin.append(nameChar[i]);

			}
		}
		return pinyin.toString();
	}

	/**
	 * 汉字转换汉语拼音全拼，英文字符不变
	 * 例如:北京 --> BEIJING
	 * @param chinese 待转换汉字
	 * @return String 全拼
	 * @author azzcsyu
	 * @update 2013-12-19 下午2:51:48
	 */
	public static String cn2Spell(String chinese) {
		StringBuilder pinyin = new StringBuilder();
		char[] nameChar = chinese.toCharArray();
		HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
		defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
		defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		for (int i = 0; i < nameChar.length; i++) {
			if (nameChar[i] > 128) {
				try {
					String[] py = PinyinHelper.toHanyuPinyinStringArray(
							nameChar[i], defaultFormat);
					if (py != null) {
						pinyin.append(py[0]);
					}
				} catch (BadHanyuPinyinOutputFormatCombination e) {
					e.printStackTrace();
				}

			} else {
				pinyin.append(nameChar[i]);
			}
		}
		return pinyin.toString();
	}

	/**
	  * 拼音转换汉语拼音，拼音首字母大写，英文字符不变
	  * 例如:北京--> BeiJing
	  * @param chinese 汉字
	  * @return String
	  */
	public static String cn2SpellFirstUpperCase(String chinese) {
		StringBuilder pinyin = new StringBuilder();
		char[] nameChar = chinese.toCharArray();
		HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
		defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
		defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		for (int i = 0; i < nameChar.length; i++) {
			if (nameChar[i] > 128) {
				try {
					String[] py = PinyinHelper.toHanyuPinyinStringArray(
							nameChar[i], defaultFormat);
					if (py != null) {
						pinyin.append(py[0].substring(0, 1).toUpperCase()
								+ py[0].substring(1));
					}
				} catch (BadHanyuPinyinOutputFormatCombination e) {
					e.printStackTrace();
				}
			} else {
				pinyin.append(nameChar[i]);
			}
		}
		return pinyin.toString();
	}

	public static void main(String[] args) {
		String name = "宝丰县12Ec小学";
	}

}
